package org.example.BIO_File_01;

import javax.naming.ldap.SortKey;
import java.io.*;
import java.net.Socket;

/**
 * BIO实现文件上传 客户端
 */
public class Client {
    public static void main(String[] args) {
        // 建立连接
        try {
            // 读取文件
            InputStream is = new FileInputStream("D:\\360MoveData\\Users\\1\\Desktop\\test.png");
            Socket socket = new Socket("127.0.0.1",9999);
            // 获取data输出流
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            dos.writeUTF(".png");
            // 读取文件并写入
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) > 0 ) {
                dos.write(buffer);
            }
            System.out.println("文件已上传");
            dos.flush();
            //通知服务端停止输出
            socket.shutdownOutput();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
